import axios from '@/api'

export interface IMember {
  iid: string
  member_id: string
  family_id: string
  s_a_id: string
  member_tel: string
  member_sfz: string
  member_nam: string
  member_sex: string
  role: string
  family_book_id: string
  member_bank_num: string
  member_bank_code: string
  readme: string
  error_msg: string
}

export interface IGetMemberList {
  request: Partial<IMember>
  response: Array<IMember>
}

export interface IGrain {
  family_id: string
  member_id: string
  s_a_id: string
  s_a_nam: string
  s_a_nam_all: string
  field: string
  field1: string
  field2: string
  plant: string
  bt_size: number
}

export interface IPlant {
  family_id: string
  s_a_id: string
  s_a_nam: string
  s_a_nam_all: string
  plant1: string
  plant2: string
  plant3: string
  plant4: string
  plant5: string
  plant6: string
  plant7: string
  plant8: string
  plant9: string
  plant10: string
  plant11: string
  plant12: string
}

/**
 * 一卡通信息导入
 *
 * @export
 * @interface IYKTAccount
 */
export interface IYKTAccount {
  member_id: string;
  member_nam: string;
  member_sfz: string;
  member_bank_num: string;
  member_bank_code: string;
}

export interface IGetMemberGrain {
  request: {
    s_a_id: string
    family_id: string
    member_sfz: string
    family_book_id: string
    member_nam: string
    member_tel: string
    member_bank_num: string
  }
  response: IGrain[]
}

export interface IField {
  family_id: string
  s_a_id: string
  field_type: string
  field_from: string
  field_size: string
  field_long: string
  field_law: string
  field_id: string
  readme: string
}

export interface IArea extends IField {
  type: string
}

export interface IGetMemberFieldDetail {
  request: Pick<IField, 'family_id' | 's_a_id' | 'field_from'>
  response: IField[]
}

// 转入
export interface IIntoField {
  field_id: string
  member_id: string
  member_nam: string
  s_a_nam_all: string
  field_size: number
  field_type: string
}
export interface IGetIntoField {
  request: Partial<Pick<IIntoField, 'field_id' | 'member_id' | 'member_nam'>> &
    Api.PageOptions
  response: IIntoField[]
}

class FamerServices {
  constructor() {}

  /**
   * 获取用户列表
   *
   * @param {IGetAdminList['request']} data
   * @returns
   * @memberof UserServices
   */
  getMemberList(data: IGetMemberList['request']) {
    return axios.post<
      unknown,
      Api.ResponseListOptions<IGetMemberList['response']>
    >('/btbusiness/farmer/get-member-list', data)
  }

  /**
   * 编辑/添加用户
   *
   * @param {IAdmin} data
   * @returns
   * @memberof UserServices
   */
  updateMember(data: IMember) {
    return axios.post<unknown, Api.ResponseOptions<IGetMemberList['response']>>(
      '/btbusiness/farmer/edit-member',
      data
    )
  }

  /**
   * 删除用户
   *
   * @param {(Pick<IMember, 'member_id' | 's_a_id'>)} data
   * @returns
   * @memberof FamerServices
   */
  deleteMember(data: Pick<IMember, 'member_id' | 's_a_id'>) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/farmer/del-member',
      data
    )
  }

  /**
   * 设置户主
   *
   * @param {(Pick<IMember, 'member_id' | 's_a_id'>)} data
   * @returns
   * @memberof FamerServices
   */
  setMemberToHolder(data: Pick<IMember, 'member_id' | 's_a_id' | 'iid'>) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/farmer/set-master',
      data
    )
  }

  /**
   * 获取用户粮食种植列表
   *
   * @param {IGetMemberGrain['request']} data
   * @returns
   * @memberof FamerServices
   */
  getMemberGrain(data: IGetMemberGrain['request']) {
    return axios.post<
      unknown,
      Api.ResponseOptions<IGetMemberGrain['response']>
    >('/btbusiness/field/get-field-list', data)
  }

  /**
   * 编辑家庭耕地信息
   *
   * @param {Partial<IField>} data
   * @returns
   * @memberof FamerServices
   */
  updateMemberGrain(data: Partial<IField>) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/field/edit-field',
      data
    )
  }

  /**
   *  删除农户耕地信息
   *
   * @param {Partial<IField>} data
   * @returns
   * @memberof FamerServices
   */
  public deleteMemberField(data: Partial<IField>) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/field/del-field',
      data
    )
  }

  /**
   * 按地区删除农户数据
   *
   * @param {Pick<IField, 's_a_id'>} data
   * @returns
   * @memberof FamerServices
   */
  public deleteMemberFromArea(data: Pick<IField, 's_a_id'>) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/farmer/del-area-farmer',
      data
    )
  }

  /**
   * 获取家庭耕地列表
   *
   * @param {IGetMemberFieldDetail['request']} data
   * @returns
   * @memberof FamerServices
   */
  getMemberFieldDetail(data: IGetMemberFieldDetail['request']) {
    return axios.post<
      unknown,
      Api.ResponseOptions<IGetMemberFieldDetail['response']>
    >('/btbusiness/field/get-field-detail', data)
  }

  /**
   * 编辑家庭种植信息
   *
   * @param {Partial<IPlant>} data
   * @returns
   * @memberof FamerServices
   */
  updateMemberPlant(data: Partial<IPlant>) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/field/edit-plant',
      data
    )
  }

  /**
   * 获取家庭种植信息
   *
   * @param {(Pick<IPlant, 'family_id' | 's_a_id'>)} data
   * @returns
   * @memberof FamerServices
   */
  getMemberPlant(
    data: Pick<IPlant, 'family_id' | 's_a_id'> & Record<string, string>
  ) {
    return axios.post<unknown, Api.ResponseOptions<IPlant[]>>(
      '/btbusiness/field/get-plant',
      data
    )
  }

  /**
   * 更新补贴面积
   *
   * @param {(Pick<IGrain, 'family_id' | 'bt_size'>)} data
   * @returns
   * @memberof FamerServices
   */
  updateBtSize(data: Pick<IGrain, 'family_id' | 'bt_size'>) {
    return axios.post<unknown, Api.ResponseOptions<IPlant[]>>(
      '/btbusiness/field/set-btsize',
      data
    )
  }

  /**
   * 导入execl用户数据
   *
   * @param {IMember[]} data
   * @returns
   * @memberof FamerServices
   */
  importExecl(params: {
    data: IMember[]
    file_nam: string
    s_a_id: string
    type?: number
  }) {
    return axios.post<Api.ResponseOptions<string[], { msg: string[] }>>(
      '/btbusiness/farmer/batch-insert-master',
      params,
      { customHandler: true }
    )
  }

  /**
   * 导c出execl农户数据
   *
   * @param {IMember[]} data
   * @returns
   * @memberof FamerServices
   */
  exportExecl(data: Record<string, unknown>) {
    return axios.post<Api.ResponseOptions<string[], { msg: string[] }>>(
      '/btbusiness/farmer/export-farmer',
      data,
      { customHandler: true, responseType: 'blob' }
    )
  }

  /**
   * 导入execl补贴面积数据
   *
   * @param {IMember[]} data
   * @returns
   * @memberof FamerServices
   */
  importGrainExecl(params: { data: IMember[]; file_nam: string }) {
    return axios.post<Api.ResponseOptions<IPlant[]> & { msg: string[] }>(
      '/btbusiness/field/batch-insert-field',
      params,
      { customHandler: true }
    )
  }

  /**
   * 导入execl家庭成员数据
   *
   * @param {IMember[]} data
   * @returns
   * @memberof FamerServices
   */
  importFamilyExecl(params: { data: IMember[]; file_nam: string }) {
    return axios.post<unknown, Api.ResponseOptions<IPlant[]>>(
      '/btbusiness/farmer/batch-insert-member',
      params
    )
  }

  /**
   * 下载模板
   *
   * @returns
   * @memberof FamerServices
   */
  downloadTemplate() {
    return axios.get<unknown, Api.ResponseOptions>(
      '/download/农户基础数据.xls',
      { responseType: 'blob', customHandler: true }
    )
  }

  /**
   * 获取可转入地列表
   *
   * @param {IGetIntoField['request']} data
   * @returns
   * @memberof LargeMemberServices
   */
  getIntoFieldsList(data: IGetIntoField['request']) {
    return axios.post<
      unknown,
      Api.ResponseListOptions<IGetIntoField['response']>
    >('/btbusiness/field/get-add-field-list', data)
  }

  /**
   * 确认转入
   *
   * @param {(Pick<IIntoField, 'field_size' | 'field_id' | 'member_id'> & { big_member_id: string, type: string })} data
   * @returns
   * @memberof LargeMemberServices
   */
  updateIntoField(
    data: Pick<IIntoField, 'field_size' | 'field_id' | 'member_id'> & {
      family_id: string
      type: string
    }
  ) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/field/edit-change-field',
      data
    )
  }

  /**
   * 同步上一年数据到本年
   *
   * @param {{s_a_id: string }} data
   * @returns
   * @memberof FamerServices
   */
  syncLastYearData(data: { s_a_id: string }) {
    return axios.post<unknown, Api.ResponseOptions>(
      '/btbusiness/farmer/export-last-year',
      data
    )
  }

  /**
   * 导入一卡通银行卡信息
   *
   * @param {{ s_a_id: string, file_nam?: string; data: IYKTAccount[] }} data
   * @returns
   * @memberof FamerServices
   */
  ImportYKTAccount(data: { s_a_id: string, file_nam?: string; data: IYKTAccount[] }) {
    return axios.post<Api.ResponseOptions<string[], { msg: string[] }>>(
      '/btbusiness/farmer/import-account',
      data,
      { customHandler: true }
    )
  }

  /**
   * 获取详细田地信息
   *
   * @param {(Api.PageOptions & Pick<IField, 'family_id' | 'field_type'> & { type: string })} data
   * @returns
   * @memberof FamerServices
   */
  getFieldDetailList(data: Api.PageOptions & Pick<IArea, 'family_id' | 'field_type' | 'type'>) {
    return axios.post<Api.ResponseOptions<IField[]>>(
      '/btbusiness/farmer/get-field-detail-list',
      data
    )
  }

  /**
   * 更新田地信息
   *
   * @param {Partial<IArea>} data
   * @returns
   * @memberof FamerServices
   */
  updateFieldDetail(data: Partial<IArea>) {
    return axios.post<Api.ResponseOptions<IField[]>>(
      '/btbusiness/farmer/set-field-detail',
      data
    )
  }

  /**
   * 删除田地信息
   *
   * @param {(Pick<IArea, 'family_id' | 'type'>)} data
   * @returns
   * @memberof FamerServices
   */
  deleteFieldDetail(data: Pick<IArea, 'family_id' | 'type'>) {
    return axios.post<Api.ResponseOptions<IField[]>>(
      '/btbusiness/farmer/del-field-detail',
      data
    )
  }

  /**
   * 获取死亡农户名单
   *
   * @param {(Pick<IMember, 'member_nam' | 'member_sfz' | 'readme'>)} data
   * @returns
   * @memberof FamerServices
   */
  getDeadMemberList(data: Pick<IMember, 'member_nam' | 'member_sfz' | 'readme'>) {
    return axios.post<Api.ResponseOptions<Pick<IMember, 'member_nam' | 'member_sfz' | 'readme'>[]>>(
      '/btbusiness/farmer/get-dead-list',
      data
    )
  }

  /**
   * 更新死亡农户数据
   *
   * @param {(Pick<IMember, 'member_nam' | 'member_sfz' | 'readme'>)} data
   * @returns
   * @memberof FamerServices
   */
  updateDeadMember(data: Pick<IMember, 'member_nam' | 'member_sfz' | 'readme'>) {
    return axios.post<Api.ResponseOptions<Pick<IMember, 'member_nam' | 'member_sfz' | 'readme'>[]>>(
      '/btbusiness/farmer/edit-dead-list',
      data
    )
  }

  /**
   * 删除死亡农户
   *
   * @param {Pick<IMember, 'iid'>} data
   * @returns
   * @memberof FamerServices
   */
  delDeadMember(data: Pick<IMember, 'iid'>) {
    return axios.post<Api.ResponseOptions>(
      '/btbusiness/farmer/del-dead-list',
      data
    )
  }

  /**
   * 获取当前区域是否有包含在死亡名单的人员
   *
   * @param {Pick<IMember, 's_a_id'>} data
   * @returns
   * @memberof FamerServices
   */
  getDeadMembersFrom(data: Pick<IMember, 's_a_id'>) {
    return axios.post<Api.ResponseOptions>(
      '/btbusiness/farmer/check-dead-list-preview',
      data
    )
  }
  
  /**
   * 从当前行政区移除已死亡名单人员
   *
   * @param {Pick<IMember, 's_a_id'>} data
   * @returns
   * @memberof FamerServices
   */
  removeDeadMembers(data: Pick<IMember, 's_a_id'>) {
    return axios.post<Api.ResponseOptions>(
      '/btbusiness/farmer/check-dead-list',
      data
    )
  }

  /**
   * 死亡农户导入接口
   *
   * @param {Api.ImportOptions<IMember[]>} data
   * @returns
   * @memberof FamerServices
   */
  ImportDeadMembers(data: Api.ImportOptions<IMember[]>) {
    return axios.post<Api.ResponseOptions>(
      '/btbusiness/farmer/import-dead-list',
      data,
      {
        customHandler: true
      }
    )
  }

  /**
   * 导出死亡农户名单
   *
   * @param {Api.ImportOptions<IMember[]>} data
   * @returns
   * @memberof FamerServices
   */
  ExportDeadMembers(data: Api.ImportOptions<IMember[]>) {
    return axios.post<Api.ResponseOptions>(
      '/btbusiness/farmer/export-dead-list',
      data,
      {
        customHandler: true,
        responseType: 'blob'
      }
    )
  }
}

export const famerServices = new FamerServices()

export default FamerServices
